/**
 * 功能:
 * 作者:周逸健
 * 日期: 2023/11/7 15:12
 */
package com.cgnfz.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cgnfz.crm.entity.dto.ApproveHistory;
import com.cgnfz.crm.entity.vo.response.ApproveResp;
import com.cgnfz.crm.entity.vo.response.item.ApproveRespItem;
import com.cgnfz.crm.mapper.ApproveHistoryMapper;
import com.cgnfz.crm.service.ApproveHistoryService;
import com.cgnfz.crm.utils.DateUtils;
import io.netty.util.internal.StringUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ApproveHistoryServiceImpl extends ServiceImpl<ApproveHistoryMapper, ApproveHistory> implements ApproveHistoryService {


    @Resource
    DateUtils dateUtils;

    /**
     * @param approveHistory
     * @return
     */
    @Override
    @Transactional
    public int insert(ApproveHistory approveHistory) {
        return this.baseMapper.insert(approveHistory);
    }

    /**
     * @param id
     */
    @Override
    @Transactional
    public void deleteByCustomerId(String id) {
        ApproveHistory approveHistory = this.baseMapper.selectOne(Wrappers.<ApproveHistory>query().eq("customer_id", id));
        if (approveHistory != null)
            this.baseMapper.deleteById(approveHistory.getId());
    }

    /**
     * @param approveHistory
     */
    @Override
    @Transactional
    public void updateByCustomerId(ApproveHistory approveHistory) {
        ApproveHistory history = this.baseMapper.selectOne(Wrappers.<ApproveHistory>query().eq("customer_id", approveHistory.getCustomerId()));
        if (history != null) {
            approveHistory.setId(history.getId());
            this.baseMapper.updateById(approveHistory);
        }
    }

    @Override
    public ApproveResp selectPage(String employeeId, String customer, String approveType, Long pageNum, Long pageSize) {
        Page<ApproveHistory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ApproveHistory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApproveHistory::getApproveEmployeeId, employeeId)
                .eq(!StringUtil.isNullOrEmpty(customer), ApproveHistory::getCustomer, customer)
                .eq(!StringUtil.isNullOrEmpty(approveType), ApproveHistory::getApproveType, approveType);
        IPage<ApproveHistory> iPage = this.baseMapper.selectPage(page, wrapper);
        List<ApproveRespItem> list = new ArrayList<>();
        for (ApproveHistory history : iPage.getRecords()) {
            ApproveRespItem item = new ApproveRespItem();
            BeanUtils.copyProperties(history, item);
            item.setApproveDate(dateUtils.getFormatDate(history.getApproveDate()));
            item.setApplyDate(dateUtils.getFormatDate(history.getApplyDate()));
            list.add(item);
        }
        return new ApproveResp(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getPages(), list);
    }

    /**
     * @param employeeId 审批人
     * @param customer 客户名
     * @param approveType 审批类型
     * @return
     */
    @Override
    public ApproveResp approveList(String employeeId, String customer, String approveType ) {

        if (StringUtil.isNullOrEmpty(employeeId)) {
            log.error("employeeId is null");
            return null;
        }
        QueryWrapper<ApproveHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("approve_employee_id", employeeId)
                .eq(!StringUtil.isNullOrEmpty(customer), "customer", customer)
                .eq(!StringUtil.isNullOrEmpty(approveType), "approve_type", approveType);
        List<ApproveHistory> approveHistories = this.baseMapper.selectList(wrapper);
        List<ApproveRespItem> list = new ArrayList<>();
        for (ApproveHistory history : approveHistories) {
            ApproveRespItem item = new ApproveRespItem();
            BeanUtils.copyProperties(history, item);
            item.setApproveDate(dateUtils.getFormatDate(history.getApproveDate()));
            item.setApplyDate(dateUtils.getFormatDate(history.getApplyDate()));
            list.add(item);
        }
        return new ApproveResp(list.size(), 1, list.size(), 1, list);
    }
}
